/**
 * @filename:SampleServiceImpl 2022-03-09 07:00:40
 * @project com/lwl/blue  V1.0
 * Copyright(c) 2018 <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a> Co. Ltd.
 * All right reserved.
 */
package com.lwl.blue.service.impl;


import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwl.blue.commont.base.Const;
import com.lwl.blue.commont.base.JsonResult;
import com.lwl.blue.commont.base.PageParam;
import com.lwl.blue.commont.utils.UserContextUtil;
import com.lwl.blue.dao.SampleDao;
import com.lwl.blue.entity.Item;
import com.lwl.blue.entity.Sample;
import com.lwl.blue.entity.SampleDetail;
import com.lwl.blue.service.ItemService;
import com.lwl.blue.service.SampleDetailService;
import com.lwl.blue.service.SampleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>自定义serviceImpl写在这里</p>
 *
 * <p>说明： 样品单资料服务实现层</P>
 *
 * @version: V1.0
 * @author: <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a>
 */
@Service
public class SampleServiceImpl extends ServiceImpl<SampleDao, Sample> implements SampleService {
    @Resource
    SampleDao sampleDao;
    @Resource
    SampleDetailService sampleDetailService;
    @Resource
    ItemService itemService;

    @Override
    public JsonResult<Page<Sample>> pages(PageParam<Sample> pageParam) {
        Page<Sample> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        return new JsonResult<Page<Sample>>().success(sampleDao.pages(page, pageParam.getParam()));
    }

    @Override
    public void deleteData(Integer id) {
        super.update(new UpdateWrapper<Sample>().lambda()
                .set(Sample::getIsDelete, Const.IS_DELETE_YES)
                .set(Sample::getUpdateUser, UserContextUtil.getUser().getUserName())
                .eq(Sample::getId, id));
        sampleDetailService.update(new UpdateWrapper<SampleDetail>().lambda()
                .set(SampleDetail::getIsDelete, Const.IS_DELETE_YES)
                .set(SampleDetail::getUpdateUser, UserContextUtil.getUser().getUserName())
                .eq(SampleDetail::getSampleId, id));
    }

    @Override
    @Transient
    public void saveUpdateData(Sample sample) {
        super.saveOrUpdate(sample);
        if (!CollectionUtils.isEmpty(sample.getItemIds())) {
            ArrayList<Item> items = (ArrayList) itemService.listByIds(sample.getItemIds());
            if (!CollectionUtils.isEmpty(items)) {
                List<SampleDetail> addList = new ArrayList(items.size());
                for (Item item : items) {
                    SampleDetail detail = new SampleDetail();
                    BeanUtils.copyProperties(item, detail);
                    detail.setSampleId(sample.getId());
                    addList.add(detail);
                }
                sampleDetailService.saveBatch(addList);
            }
        }
    }
}
